<?php
namespace app\index\controller;

use think\Controller;
use think\Db;

class Index extends Controller
{
    public function index()
    {
        $data = [];

        return view('index/index');
    }

    public function get_data()
    {
        $data = [];

        $data = Db::table('trace')->where('status', 1)->order('id desc')->limit(20)->select();

        if(!$data) $data = [];

        return json($data);
    }

    public function hello()
    {
        $data = [];

        return view('index/hello', $data);
    }

    public function add_mark()
    {
        $param = request()->param();
        $back  = ['code' => 1, 'msg' => '', 'id' => 0];
        if (!key_exists('time', $param) || $param['time'] == '') {
            $back['code'] = 0;
            $back['msg']  = '时间不能为空';
            return json($back);
        }
        if (!key_exists('who', $param) || $param['who'] == '') {
            $back['code'] = 0;
            $back['msg']  = '参与者不能为空';
            return json($back);
        }
        if (!key_exists('event', $param) || $param['event'] == '') {
            $back['code'] = 0;
            $back['msg']  = '时间不能为空';
            return json($back);
        }
        if (!key_exists('pointX', $param) || $param['pointX'] == '') {
            $back['code'] = 0;
            $back['msg']  = '横坐标不能为空';
            return json($back);
        }
        if (!key_exists('pointY', $param) || $param['pointY'] == '') {
            $back['code'] = 0;
            $back['msg']  = '纵坐标不能为空';
            return json($back);
        }
        $data = [];
        $data['event_time'] = $param['time'];
        $data['event_desc'] = $param['event'];
        $data['event_part'] = $param['who'];
        $data['event_x'] = $param['pointX'];
        $data['event_y'] = $param['pointY'];
        $data['ip']      = get_IP();
        $data['add_time'] = date('Y-m-d H:i:s');
        $id = Db::table('trace')->insertGetId($data);
        $back['id'] = $id;
        if (!$back['id']) {
            $back['code'] = 0;
            $back['msg']  = '插入失败请重试';
        }

        return json($back);
    }

    public function del_mark()
    {
        $param = request()->param();
        $back  = ['code' => 1, 'msg' => '', 'id' => 0];
        if (!key_exists('id', $param) || $param['id'] == '') {
            $back['code'] = 0;
            $back['msg']  = '数据异常';
            return json($back);
        }
        $row = Db::table('trace')->find($param['id']);
        if (!is_array($row) || count($row) < 1) {
            $back['code'] = 0;
            $back['msg']  = '数据不存在';
            return json($back);
        }
        Db::table('trace')->delete($param['id']);

        return json($back);
    }
}

